using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace GuavaData
{
    public class RowConstant : Operator
    {
        #region Fields
        List<object> fields;
        public List<object> Fields
        {
            get { return fields; }
            set { fields = value; }
        }
        #endregion Fields

        public RowConstant()
        {
            // Populate the external view and the fields list
            this.exposedColumns = new List<string>();
            this.exposedDomains = new List<Domain.Domain>();
            this.fields = new List<object>();
        }

        public RowConstant(IEnumerable<string> columnAliases, IEnumerable<Domain.Domain> domains, IEnumerable<object> fields)
        {
            // Populate the external view and the fields list
            this.exposedColumns = new List<string>(columnAliases);
            this.exposedDomains = new List<Domain.Domain>(domains);
            this.fields = new List<object>(fields);

            // Dummy check
            if (this.exposedColumns.Count != this.exposedDomains.Count)
                throw new Exception("Number of columns does not match number of domains.");
            if (this.exposedColumns.Count != this.fields.Count)
                throw new Exception("Number of columns does not match number of values.");
        }

        public void AddValue(string columnAlias, Domain.Domain domain, object field)
        {
            this.exposedColumns.Add(columnAlias);
            this.exposedDomains.Add(domain);
            this.fields.Add(field);
        }

        public override SqlText Visit(Provider qv)
        {
            return qv.Transform(this);
        }
        public override void Visit(Transform t, Command c)
        {
            t.Expand(this, c);
        }

        public override Operator Clone()
        {
            return new RowConstant(this.exposedColumns, this.exposedDomains, this.fields);
        }

        public override void NotifyChange(Operator old, Operator newOp)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public string AsColumnText
        {
            get
            {
                string temp = "";
                for (int i = 0; i < exposedColumns.Count; i++)
                    temp = String.Concat(temp, Common.Wrap(fields[i]), " AS ", exposedColumns[i], ", ");
                return temp.Substring(0, temp.Length - 2);
            }
        }

        public override void Visit(Channel c, string table)
        {
            c.MapAliases(this, table);
        }

        }
}
